package com.sl.springboot.controller;

import com.sl.springboot.model.Coach;
import com.sl.springboot.model.Course;
import com.sl.springboot.model.PageInfo;
import com.sl.springboot.model.User;
import com.sl.springboot.service.CoachService;
import com.sl.springboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class CourseController {

    // 依赖注入
    @Autowired
    private CourseService courseService;

    @Autowired
    private CoachService coachService;


    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findCourse")
    public String findCourse(Integer pageIndex, Integer pageSize,String name,String money, Model model,HttpServletRequest request)  throws Exception{
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        String type = (String)session.getAttribute("type");
        Map mp = new HashMap();
        mp.put("name",name);
        mp.put("money",money);
        if(type.equals("02")){
            User u = (User)session.getAttribute("ad");
            mp.put("uid",u.getId());
        }else if(type.equals("03")){
            Coach coach = (Coach) session.getAttribute("ad");
            mp.put("instructor",coach.getUsername());
        }
        PageInfo<Course> pageList = courseService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "CourseList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addCourse" ,method = RequestMethod.POST)
    @ResponseBody
    public String addCourse( @RequestBody Course course) {
        //验证账号是否存在
        Map<String,Object> map = new HashMap<>();
        map.put("username",course.getInstructor());
        List<Coach> list = coachService.queryFilter(map);
        if(list.size()<=0){
            return "201";
        }
        int d = courseService.addCourse(course);
        return "CourseList";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteCourse")
    @ResponseBody
    public String deleteCourse(String id) {
        int d = courseService.deleteCourse(id);
        return "CourseList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateCourse")
    public String updateCourse( Course course) {
        int d = courseService.updateCourse(course);
        return "redirect:/findCourse";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findCourseById")
    public String findCourseById(String id,Model model,HttpServletRequest request) {
        Course course= courseService.findCourseById(id);
        model.addAttribute("course",course);
        return "CourseEdit";
    }


}
